let TICK_RATE = 100;
let FPS = 120;
let TILE_SIZE = 50;

//enums
const gameStates = {
	MENU: 0,
	IN_GAME: 1
}

const orderTypes = {
	IDLE: 0,
	MOVE: 1
}
//objects
function Vector2d(x, y) {
	this.x = x;
	this.y = y;
}

Vector2d.prototype.sumVector2d = function(otherVector2d) {
	return new Vector2d(this.x + otherVector2d.x, this.y + otherVector2d.y);
}

Vector2d.prototype.distanceTo = function(otherVector2d) {
	if(!otherVector2d) return 999999;
	return Math.sqrt(Math.pow(this.x - otherVector2d.x, 2) + Math.pow(this.y - otherVector2d.y, 2));
}

Vector2d.prototype.multByScalar = function(scalar) {
	return new Vector2d(this.x * scalar, this.y * scalar);
}

Vector2d.prototype.goDistanceTowardsVector2d = function(otherVector2d, distanceTowardsVector2d) {
	let xDifference = otherVector2d.x - this.x;
	let yDifference = otherVector2d.y - this.y;

	let distanceBetweenThisAndOtherVector2d = Math.sqrt(xDifference* xDifference + yDifference * yDifference);
	if(distanceBetweenThisAndOtherVector2d == 0) return this;

	let xNormalized = xDifference * (distanceTowardsVector2d / distanceBetweenThisAndOtherVector2d);
	let yNormalized = yDifference * (distanceTowardsVector2d / distanceBetweenThisAndOtherVector2d);

	return new Vector2d(this.x + xNormalized, this.y + yNormalized);
}

function Order(player, orderType, target, arrayOfUnitIds, additionalData) {
	this.player = player;
	this.orderType = orderType;
	this.target = target;
	this.arrayOfUnitIds = arrayOfUnitIds;
	this.additionalData = additionalData;
}

function Unit(owner, pos, movementSpeed = 1, collisionRadius = 25){
	this.owner = owner;
	this.id = currentGame.unitId++;
	this.pos = pos;
	this.orders = [];
	this.currentOrder;
	this.currentOrderTarget;
	
	//movement stuff
	this.movementSpeed = movementSpeed;
	this.collisionRadius = collisionRadius;
	this.pathCalculated = false;
	this.blocked = 0;
	this.path = []; //array of locations to path through
	this.arrivalDistance = 0; //at some point just say 'gud nuff'
}

Unit.prototype.draw = function() {
	ctx.beginPath();
	ctx.arc(this.pos.x, this.pos.y, 10, 0, 2 * Math.PI);
	ctx.stroke();
}

Unit.prototype.getClosestAdjecentTile = function() {
	let currentTile = currentGame.getTileFromPixelXY(this.pos.x, this.pos.y);
	let currentTileTopRight = currentTile.multByScalar(TILE_SIZE);
	let gridx;
	let gridy;
	if(this.pos.x > currentTileTopRight.x + TILE_SIZE/2) {
		gridx = 1;
	} else {
		gridx = -1;
	}
	if(this.pos.y > currentTileTopRight.y + TILE_SIZE/2) {
		gridy = 1;
	} else {
		gridy = -1;
	}
	return new Vector2d(currentTile.x + gridx, currentTile.y + gridy);
}

Unit.prototype.getClosestPointToTile = function(tile) {
	let clamp = function(cc, rc1, rc2) {
		return cc < rc1 ? rc1 : cc > rc2 ? rc2 : cc;
	}
	return new Vector2d(clamp(this.pos.x, (tile.x * TILE_SIZE), (tile.x * TILE_SIZE) + TILE_SIZE), clamp(this.pos.y, (tile.y * TILE_SIZE), (tile.y * TILE_SIZE) + TILE_SIZE));
}

Unit.prototype.getClosestAdjecentWallTile = function() {
	let offsets = [
		[1, 1], [0, 1], [-1, 1],
		[1, 0], [0, 0], [-1, 0],
		[1, -1], [0, -1], [-1, -1],
	]
	let currentTile = currentGame.getTileFromPixelXY(this.pos.x, this.pos.y);
	let closestPoint = new Vector2d(9999999999, 9999999999);
	let closestTile = closestPoint;
	for(let i = 0; i < offsets.length; i++) {
		let testTile = new Vector2d(currentTile.x + offsets[i][0], currentTile.y + offsets[i][1]);
		if(testTile.x < 0 || testTile.x > currentGame.map[0].length - 1 || testTile.y < 0 || testTile.y > currentGame.map.length)
			continue;
		let closestPointToTestTile = this.getClosestPointToTile(testTile);
		let distanceToCPTTT = this.pos.distanceTo(closestPointToTestTile);
		if(currentGame.map[testTile.y][testTile.x] == 1 && this.pos.distanceTo(closestPoint) > distanceToCPTTT) {
			closestPoint = closestPointToTestTile;
			closestTile = testTile;
		}
	}
	return closestTile;
}

Unit.prototype.canIStandHere = function(targetPos) {
	let targetTile = currentGame.getTileFromPixelXY(targetPos.x, targetPos.y);
	let closestWallTile = this.getClosestAdjecentWallTile();
	if(!closestWallTile || currentGame.map[targetTile.y][targetTile.x] != 0) return true;
	let closestPoint = this.getClosestPointToTile(closestWallTile);
	if(this.pos.distanceTo(closestPoint) < this.collisionRadius) {
		return false;
	} else {
		return true;
	}
}

Unit.prototype.getCollidingUnits = function() {
	let collidingUnits = [];
	for(let i = 0; i < currentGame.units.length; i++) {
		let unit = currentGame.units[i];
		if(this.pos.distanceTo(unit.pos) < this.collisionRadius && unit != this) {
			collidingUnits.push(unit);
		}
	}
	return collidingUnits;
}

Unit.prototype.collision = function() {
	let nearestWall = this.getClosestAdjecentWallTile();
	while(!this.canIStandHere(this.pos)) {
		console.log(this.pos.x, this.pos.y, this.id);
		this.pos = this.pos.goDistanceTowardsVector2d(new Vector2d((nearestWall.x * TILE_SIZE) + 25, (nearestWall.y * TILE_SIZE) + 25), -1)
	}
	let collidingUnits = this.getCollidingUnits();
	for(let i = 0; i < collidingUnits.length; i++) {
		let unit = collidingUnits[i];
		//at targetPos would i be intersecting a wall?
		this.pos = this.pos.goDistanceTowardsVector2d(unit.pos, -1);
	}
}

Unit.prototype.update = function() {
	this.collision();
	switch(this.currentOrder) {
		case orderTypes.MOVE:
			if(this.path.length) {
				if(this.pos.distanceTo(this.path[0]) < this.arrivalDistance) {
					this.path.shift();
				} else { 
					let targetPos = this.pos.goDistanceTowardsVector2d(this.path[0], this.movementSpeed);
					let collidingUnits = this.getCollidingUnits();
					if(collidingUnits.length)
						this.blocked++;
					if(this.blocked > 100)
						this.currentOrder = orderTypes.IDLE;
					else
						this.pos = targetPos;
				}
			} else if(!this.pathCalculated) {
				return;
			} else {
				this.currentOrder = orderTypes.IDLE;
		//		currentGame.sendOrder(this.owner, orderTypes.IDLE, {}, [unitID] {});
			}
			break;
		case orderTypes.IDLE:
			if(this.orders.length) {
				this.currentOrder = this.orders.shift();
			}
			break;
		default:
			break;

	}
}

function Player(name) {
	this.name = name;
	this.id = currentGame.playerId++;
	this.units = [];
}

function Game() {
	this.singlePlayer = true;
	this.tickCounter; //keeps track of what tick we are on
	this.gameState; //game state
	this.players = []; //keeps track of players
	this.playerId = 0;
	this.player;
	this.units = []; //keeps track of units
	this.unitsById = {};
	this.unitId = 0; //provides identification for units
	this.selectedUnits = [] //keeps track of selected units
	
	this.pathFinder = new EasyStar.js();
	this.pathFinder.enableDiagonals();
}

Game.prototype.loadMap = function(map) {
	this.map = map;
	this.pathFinder.setGrid(map);
	this.pathFinder.setAcceptableTiles([0]);
}

Game.prototype.setPlayer = function(player) {
	this.player = player;
}

Game.prototype.getUnitsFromBox = function(x1, y1, x2, y2) {
	let unitsInBox = [];
	let temp;
	if(x2 < x1) {
		temp = x1;
		x1 = x2;
		x2 = temp;
	}
	if(y2 < y1) {
		temp = y1;
		y1 = y2;
		y2 = temp;
	}
	for(let i = 0; i < this.units.length; i++) {
		if(	x1 <= this.units[i].pos.x && 
			x2 >= this.units[i].pos.x && 
			y1 <= this.units[i].pos.y && 
			y2 >= this.units[i].pos.y 
		) {
			unitsInBox.push(this.units[i]);
		}
	}
	return unitsInBox;
}

Game.prototype.registerUnit = function(unit) {
	this.units.push(unit);
	this.unitsById[unit.id] = unit;
	unit.owner.units.push(unit);
}

Game.prototype.registerPlayer = function(player) {
	this.players.push(player);
}

Game.prototype.getUnitById = function(id) {
	return this.unitsById[id];
}

Game.prototype.getIdsOfUnitsFromArray = function(arrayOfUnits) {
	let unitIdArray = [];
	for(let i = 0; i < arrayOfUnits.length; i++) {
		unitIdArray.push(arrayOfUnits[i].id);
	}
	return unitIdArray;
}

Game.prototype.getCenterOfUnits = function(arrayOfUnits) {
	let x = 0;
	let y = 0;
	arrayOfUnits.forEach(function (unit) {
		x += unit.pos.x;
		y += unit.pos.y;
	});
	x /= arrayOfUnits.length;
	y /= arrayOfUnits.length;
	return new Vector2d(x, y);
}

Game.prototype.getUnitFromVector2d = function(vector2d) {
	for(let i = 0; i < this.units.length; i++)
		if(this.units[i].pos.distanceTo(vector2d) < 20)
			return this.units[i];
}

Game.prototype.sendOrder = function(order) {
	if(this.singlePlayer) {
		currentSinglePlayerHandler.sendOrder(order);
	} else {
		currentMultiPlayerHandler.sendOrder(order);
	}
}

Game.prototype.getTileFromPixelXY = function(px, py) {
	for(let y = 0; y < map.length; y++) {
		for(let x = 0; x < map[y].length; x++) {
			if(	x * TILE_SIZE <= px && 
				(x * TILE_SIZE) + TILE_SIZE>= px && 
				y * TILE_SIZE <= py && 
				(y * TILE_SIZE) + TILE_SIZE >= py 
			) {
				return new Vector2d(x, y);
			}
		}
	}
}

Game.prototype.processOrder = function(order) {
	switch(order.orderType) {
		case orderTypes.MOVE:
			order.arrayOfUnitIds.forEach(function (unitId) {
				if(order.player == currentGame.unitsById[unitId].owner) {
					if(order.target.distanceTo(currentGame.unitsById[unitId].path[currentGame.unitsById[unitId].path.length - 1]) > 30) {
						currentGame.unitsById[unitId].blocked = 0;
						currentGame.unitsById[unitId].pathCalculated = false;
						currentGame.unitsById[unitId].currentOrder = orderTypes.MOVE;
						let currentTile = currentGame.getTileFromPixelXY(currentGame.unitsById[unitId].pos.x, currentGame.unitsById[unitId].pos.y);
						let targetTile = currentGame.getTileFromPixelXY(order.target.x, order.target.y);
						currentGame.pathFinder.findPath(currentTile.x, currentTile.y, targetTile.x, targetTile.y, function(path) {
							if(path) {
								this.pathCalculated = true;
								currentGame.unitsById[unitId].path = [];
								for(let i = 0; i < path.length - 1; i++) {
									let pathStep = new Vector2d(path[i+1].x, path[i+1].y);
									pathStep = pathStep.multByScalar(TILE_SIZE);
										pathStep = pathStep.sumVector2d(new Vector2d(25, 25));
										currentGame.unitsById[unitId].path[i] = pathStep;
									}
								}
							});
							currentGame.pathFinder.calculate();
							currentGame.unitsById[unitId].arrivalDistance = order.additionalData.arrivalDistance;
						}
					}
				});
				break;
			case orderTypes.IDLE:
				order.arrayOfUnitIds.forEach(function (unitId) {
					if(order.player == currentGame.unitsById[unitId].owner) {
						currentGame.unitsById[unitId].path = [];
						currentGame.unitsById[unitId].arrivalDistance = 0;
					}
				});
				break;
			default:
				break;
		}
	}

	Game.prototype.update = function() {
	this.units.forEach(function (unit) {
		unit.update();
	});
}

Game.prototype.draw = function() {
	ctx.clearRect(0, 0, mainCanvas.width, mainCanvas.height);
	let currentTile = this.getTileFromPixelXY(currentInputManager.mouseX, currentInputManager.mouseY);
	ctx.strokeRect(currentTile.x * TILE_SIZE, currentTile.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
	for(let y = 0; y < map.length; y++) {
		for(let x = 0; x < map[y].length; x++) {
			if(map[y][x] == 1) {
				ctx.fillStyle = 'rgba(0, 0, 0, 1)';
				ctx.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
			}
		}
	}
	if(currentInputManager.selectionBox)
		currentInputManager.drawSelectionBox();
	for(let i = 0; i < this.units.length; i++)
		this.units[i].draw();
	for(let i = 0; i < this.selectedUnits.length; i++) {
		if(this.selectedUnits[i]) {
			ctx.strokeStyle = 'rgba(0, 0, 0, 0.4)';
			ctx.strokeRect(this.selectedUnits[i].pos.x - 15, this.selectedUnits[i].pos.y - 15, 30, 30);
			ctx.strokeStyle = 'red';
			let closestTile = this.selectedUnits[i].getClosestAdjecentWallTile();
			ctx.strokeRect(closestTile.x * TILE_SIZE, closestTile.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
			ctx.strokeStyle = 'rgba(0, 0, 0, 1)';
		}
	}
}

function singlePlayerHandler() {
	this.outgoingOrders = [];
	this.incomingOrders = [];
}

singlePlayerHandler.prototype.sendOrder = function(order) {
	currentGame.processOrder(order);
}

function InputManager() {
	this.keys = new Array(300);

	this.selectionBox = false;
	this.mouseX = 0;
	this.mouseY = 0;
	this.startX = 0; //used to draw selection box
	this.startY = 0; //used to draw selection box
}

InputManager.prototype.drawSelectionBox = function() {
	ctx.strokeRect(this.startX, this.startY, this.mouseX - this.startX, this.mouseY - this.startY);
}

InputManager.prototype.getKeyCode = function(e) {
	return e.which || e.keycode;
}

//event listeners
function resizeCanvas() { 
	mainCanvas.width = window.innerWidth;
	mainCanvas.height = window.innerHeight;
}

function handleKeyDown(e) {
	//worry about this later
}

function handleKeyUp(e) {
	//worry about this later
}

function handleMouseMove(e) {
	let doc = document.documentElement;
	let body = document.body;
	currentInputManager.mouseX = e.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
	currentInputManager.mouseY = e.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
}

function handleMouseDown(e) {
	if(currentInputManager.getKeyCode(e) === 1) {
		currentGame.selectedUnits = [];
		currentInputManager.selectionBox = true;
		currentInputManager.startX = currentInputManager.mouseX;
		currentInputManager.startY = currentInputManager.mouseY;
		let unit = currentGame.getUnitFromVector2d(new Vector2d(currentInputManager.mouseX, currentInputManager.mouseY));
		if(unit) {
			currentGame.selectedUnits.push(unit);
		}
	} else if (currentInputManager.getKeyCode(e) === 3) {
		e.preventDefault();
		let maxDistanceFromCenter = 0;
		let centerOfSelectedUnits = currentGame.getCenterOfUnits(currentGame.selectedUnits);
		for(let i = 0; i < currentGame.selectedUnits.length; i++) {
			maxDistanceFromCenter = Math.max(maxDistanceFromCenter, currentGame.selectedUnits[i].pos.distanceTo(centerOfSelectedUnits));
		}
		if(maxDistanceFromCenter > 10) maxDistanceFromCenter = 20;
		currentGame.sendOrder(new Order(currentGame.player, orderTypes.MOVE, new Vector2d(currentInputManager.mouseX, currentInputManager.mouseY), currentGame.getIdsOfUnitsFromArray(currentGame.selectedUnits), {arrivalDistance: (maxDistanceFromCenter <= 2) ? 5 : maxDistanceFromCenter}));
	}
}

function handleMouseUp(e) {
	if(currentInputManager.getKeyCode(e) === 1) {
		if(Math.abs(currentInputManager.startY - currentInputManager.mouseY) > 10) {
			currentGame.selectedUnits = currentGame.getUnitsFromBox(currentInputManager.startX, currentInputManager.startY, currentInputManager.mouseX, currentInputManager.mouseY);
		}
		currentInputManager.selectionBox = false;
		currentInputManager.startX = 0;
		currentInputManager.startY = 0;
	} else if (currentInputManager.getKeyCode(e) === 3) {
		//nothing
	}
}

const mainCanvas = document.getElementById('mainCanvas');
const ctx = mainCanvas.getContext('2d');
resizeCanvas();

//game init
let currentGame = new Game();
let currentInputManager = new InputManager();
let currentSinglePlayerHandler = new singlePlayerHandler();

let map = [
	[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
	[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
	[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
	[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
	[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
	[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
	[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
	[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
	[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
];

currentGame.loadMap(map);

let playingPlayer = new Player('test');
currentGame.registerPlayer(playingPlayer);
currentGame.setPlayer(playingPlayer);

let spawnPoint = new Vector2d(100, 100);
let testUnit = new Unit(playingPlayer, spawnPoint);
currentGame.registerUnit(testUnit);

//event listener setup
window.addEventListener('resize', resizeCanvas);
mainCanvas.addEventListener('mousedown', handleMouseDown);
mainCanvas.addEventListener('mousemove', handleMouseMove);
mainCanvas.addEventListener('mouseup', handleMouseUp);
mainCanvas.addEventListener('contextmenu', function(e) { e.preventDefault(); });

let drawInteraval = setInterval(function() {currentGame.draw()}, FPS/1000);
let updateInterval = setInterval(function() {currentGame.update()}, 100/1000);
//setTimeout(clearInterval(poop), 10000);
//currentGame.draw();
